# -*- coding: UTF-8 -*-
import os
import re
disable_trigger_demo = f"sqlcmd -Q \"DISABLE TRIGGER sales.trg_members_insert ON sales.members\""

# 递归遍历目录
def traversal_files(path, templist):
    for dir in os.listdir(path):
        dir = os.path.join(path, dir)
        dir = dir.replace("\\", "/")

        # print(dir)
        # 判断当前目录是否为文件夹
        if os.path.isdir(dir):
            traversal_files(dir, templist)
        else:
            dir_words = dir.split("/")
            dir_index = len(dir_words) - 1
            cur_dir = str(dir_words[dir_index])
            if ("mssql" in cur_dir):
                tab_names = re.findall(r"(?<=4).*(?=2)", cur_dir)
                tab_name = ""
                if len(tab_names) > 0:
                    tab_name = tab_names[0]
                tab_action = cur_dir.split("_")[-1]
                templist.append(f"sqlcmd -d %dbName% -Q \"DISABLE TRIGGER tr_{tab_action}_{tab_name} ON {tab_name}\"\n")


if __name__ == '__main__':
    path = "C:/APS/export/mapping"
    templist = []
    traversal_files(path, templist)
    temp = ""
    for t in templist:
        temp += t

    mssql_trigger_drop_bat_template = f"@ECHO OFF\n" \
                                        f"set dbName=DB_KAER\n" \
                                        f"{temp}" \
                                        f"ECHO\n" \
                                        f"PAUSE\n" \
                                        f"@ECHO Done"
    print(mssql_trigger_drop_bat_template)


